package com.flashcards.vocabulary;

import com.flashcards.auth.OAuthConstant;
import com.flashcards.repository.UserInfoRepository;
import com.flashcards.LogWrapper;
import com.flashcards.Word;
import weibo4andriod.Status;
import weibo4andriod.Weibo;
import weibo4andriod.WeiboException;

import java.util.List;

public class RemoteVocabulary {
    private UserInfoRepository userInfoRepository;
    private OAuthConstant constant;

    public RemoteVocabulary() {
        this(UserInfoRepository.INSTANCE(), OAuthConstant.getInstance());
    }

    public RemoteVocabulary(UserInfoRepository repository, OAuthConstant constant) {
        this.userInfoRepository = repository;
        this.constant = constant;
    }

    public Vocabulary lookUp() {
        Vocabulary vocabulary = new Vocabulary();
        Weibo weibo = userInfoRepository.lookup().initialize(constant.getWeibo());
        try {
            List<Status> friendsTimeline = weibo.getUserTimeline();
            for (Status status : friendsTimeline) {
                LogWrapper.i(">>>", "status - " + status.getText());
                vocabulary.add(Word.parseFromTwitteSyntax(status.getText()).translate());
            }
            return vocabulary;
        } catch (WeiboException e) {
            LogWrapper.i("＝＝", "Failed to login the WeiBo", e);
            return vocabulary;
        }
    }
}